package com.lsfb.duishangbaos.Code;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lsfb.duishangbaos.Operate.SetMoneyActivity;
import com.lsfb.duishangbaos.Operate.SetMoneyClickEvent;
import com.lsfb.duishangbaos.Operate.SetMoneyClickEvent2;
import com.lsfb.duishangbaos.R;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;

import java.util.Hashtable;

/**
 * 二维码收款
 * Created by Administrator on 2017/7/25 0025.
 */
@ContentView(R.layout.aty_code_money)
public class CodeMoneyActivity extends LsfbActivity {

    /**
     * 金额
     */
    @ViewInject(R.id.aty_text_money)
    private TextView text_money;

    @ViewInject(R.id.aty_img_erweima)
    private ImageView img_erweima;


    private String money = "";

    private Bitmap qrBitmap;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1000:
                    text_money.setVisibility(View.VISIBLE);
                    text_money.setText(money);
                    break;
                case 2000:
                    img_erweima.setImageBitmap(qrBitmap);
                    break;
            }
        }
    };

    @Override
    public void initView() {
        //改变系统状态栏颜色
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = new View(window.getContext());
        int statusBarHeight = getStatusBarHeight(window.getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
        params.gravity = Gravity.TOP;
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(getResources().getColor(R.color.color_yellow));
        decorViewGroup.addView(statusBarView);
    }

    /**
     * 计算系统状态栏高度
     *
     * @param context
     * @return
     */
    private int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_back, R.id.aty_text_set_money, R.id.aty_text_my_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_back:
                finish();
                break;
            //设置金额
            case R.id.aty_text_set_money:
                startActivity(new Intent(this, SetMoneyActivity.class));
                break;
            //我的二维码
            case R.id.aty_text_my_code:
               startActivity(new Intent(this,MyCodeActivity.class));
                break;
        }
    }

    @Override
    public void initData() {

    }

    public Bitmap createQRImage(String url, final int width, final int height) {
        try {
            // 判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url,
                    BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            // 下面这里按照二维码的算法，逐个生成二维码的图片，
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = 0xff000000;
                    } else {
                        pixels[y * width + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取密文
     *
     * @param event
     */
    @EventAnnotation
    public void SetMoneyClickEvent(SetMoneyClickEvent event) {
        qrBitmap = createQRImage(event.getData(), 500, 500);

        handler.sendEmptyMessage(2000);
    }

    /**
     * 获取金额
     *
     * @param event
     */
    @EventAnnotation
    public void SetMoneyClickEvent2(SetMoneyClickEvent2 event) {
        money = event.getData();
        handler.sendEmptyMessage(1000);
    }

}
